<?php

namespace App\Http\Controllers\Api\Passengers;

use App\Http\Controllers\Controller;
use App\Http\Resources\Passengers\PassengerResource;
use App\Http\Resources\Passengers\PassengersResource;
use App\Models\Passengers\Passenger;
use App\Models\Passengers\Repositories\Interfaces\PassengerRepositoryInterface;
use App\Trip\Orders\UserLastOrder;
use App\Trip\Passengers\Passengers;
use Exception;
use Illuminate\Http\Request;

class PassengerController extends Controller
{


    private PassengerRepositoryInterface $passengerRepo;
    private string|null                  $user_id;

    public function __construct(
        PassengerRepositoryInterface $passengerRepository,
    )
    {
        $this->passengerRepo = $passengerRepository;
        $this->user_id       = auth()->id();
    }

    public function list(Request $request)
    {
        $list = $this->passengerRepo->listAll([
            'user_id' => $this->user_id,
        ]);
        return $this->success('', ['passengers' => PassengersResource::collection($list)]);
    }

    public function defaults()
    {
        // 默认乘客 ids
        $passenger_ids = UserLastOrder::getLastOrderPassengersIds($this->user_id);
        $limit         = 1;//empty($passenger_ids) ? 1 : 0;
        /**
         * 默认数据如果不存在 ，提取第一个乘客信息
         */
        $list = $this->passengerRepo->listAll([
            'passenger_ids'  => $passenger_ids,
            'user_id'        => $this->user_id,
            'passenger_type' => [Passengers::PASSENGER_TYPE_ADULT, Passengers::PASSENGER_TYPE_STUDENT],
            'verify_state'   => Passenger::VERIFY_STATE_VERIFIED,
        ],
            $limit
        );
        return $this->success('', [
            'passengers' => PassengersResource::collection($list),
            'all'        => PassengersResource::collection($this->passengerRepo->listAll([
                'user_id' => $this->user_id,
            ])),
        ]);
    }

    public function store(Request $request)
    {
        $data            = $request->all();
        $data['user_id'] = $this->user_id;
        $passenger       = $this->passengerRepo->addPassenger($data);
        return $this->success('success', [
            'passenger_id' => $passenger->getPassengerId(),
        ]);
    }

    public function update(string $passenger_id, Request $request)
    {
        $data            = $request->all();
        $passenger       = $this->passenger($passenger_id);
        $data['user_id'] = $this->user_id;
        if (!$passenger) {
            if (!$passenger || $passenger->isDeleted()) {
                return $this->error(404, '乘客信息不存在');
            }
        } else {
            $this->passengerRepo->updatePassenger($passenger, $data);
        }
        return $this->success('success');
    }

    private function passenger(string $passenger_id)
    {
        return $this->passengerRepo->findOneByPassengerIdAndUserId($passenger_id, $this->user_id);
    }

    public function show(string $passenger_id)
    {
        $passenger = $this->passenger($passenger_id);
        if (!$passenger || $passenger->isDeleted()) {
            return $this->error(404, '乘客信息不存在');
        }
        return $this->success('', new PassengerResource($passenger));
    }

    /**
     * @throws Exception
     */
    public function delete(string $passenger_id)
    {
        $passenger = $this->passenger($passenger_id);
        if (!$passenger) {
            return $this->error(404, '乘客信息不存在');
        }
        $this->passengerRepo->destroy($passenger);
        return $this->success();
    }
}
